import 'dart:ui';

import 'package:flutter/cupertino.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter/src/widgets/framework.dart';
import 'package:image_gallery_saver/image_gallery_saver.dart';
import 'package:qr_code_app/common/constant/color_config.dart';
import 'package:qr_code_app/common/util/util.dart';
import 'package:qr_code_app/generated/assets/qr_code_app_assets.dart';
import 'package:qr_code_app/module/diary/entity/log_entity.dart';
import 'package:qr_flutter/qr_flutter.dart';
import 'package:screenshot/screenshot.dart';
import 'package:todo_flutter/todo_flutter.dart';

class LogItemWidget extends BaseStatefulWidget {
  final LogEntity entity;

  const LogItemWidget(this.entity, {super.key});

  @override
  State<StatefulWidget> createState() => _LogItemWidgetState();
}

class _LogItemWidgetState extends BaseState<LogItemWidget> {
// 截图控制器
  final ScreenshotController screenshotController = ScreenshotController();

  Widget get qrCode => QrImageView(
        data: '${widget.entity.content ?? ''}\n${widget.entity.timeStr}',
        version: QrVersions.auto,
        size: 200.0,
      );

  @override
  Widget build(BuildContext context) {
    return Padding(
      padding: all(16),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Row(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Expanded(
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    CommonText(
                      'Persephone',
                      fontSize: 16,
                      fontWeight: FontWeight.w500,
                      color: ColorsConfig.primaryColor,
                    ),
                    CommonText(
                      widget.entity.content ?? '',
                      fontSize: 16,
                      color: ColorsConfig.ff161619,
                      maxLines: 99,
                    ),
                    SizedBox(
                      height: 12,
                    ),
                    CommonImage(
                      memory: Util().stringToU8(widget.entity.image ?? ''),
                      size: 300,
                    ),
                    SizedBox(
                      height: 12,
                    ),
                    Row(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      children: [
                        CommonText(
                          widget.entity.timeStr,
                          fontSize: 14,
                          color: ColorsConfig.mainTextColorA9,
                        ),
                        CommonClickWidget(
                          singleClick: () {
                            showQRCodeDialog(
                              context,
                              contentStr: widget.entity.content ?? '',
                              timeStr: widget.entity.timeStr,
                            );
                          },
                          child: CommonImage(
                            asset: QrCodeAppAssets.qrIc,
                            size: 30,
                          ),
                        ),
                      ],
                    ),
                  ],
                ),
              )
            ],
          ),
        ],
      ),
    );
  }

  // 生成带有 content 和 timeStr 的二维码图片
  Widget generateQRCodeDialogContent(
    BuildContext context, {
    required String contentStr,
    required String timeStr,
  }) {
    // 创建二维码图片

    return SizedBox(
      height: 300,
      child: Column(
        children: [
          SizedBox(height: 20),
          Expanded(child: qrCode), // 显示二维码图片
          SizedBox(height: 20),
          CommonText('扫描二维码查看内容'),
        ],
      ),
    );
  }

  // 显示带有 content 和 timeStr 的二维码图片对话框
  Future<bool?> showQRCodeDialog(
    BuildContext context, {
    required String contentStr,
    required String timeStr,
  }) async {
    return Util.showContentDialogV2<bool>(
      context,
      content: Column(
        children: [
          generateQRCodeDialogContent(
            context,
            contentStr: contentStr,
            timeStr: timeStr,
          ),
          SizedBox(height: 20),
          Padding(
            padding: EdgeInsets.only(left: 38, right: 38, bottom: 20),
            child: Row(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                Expanded(
                  child: CommonButton(
                    '取消',
                    height: 40,
                    fontSize: 14,
                    textColor: ColorsConfig.ff8a919f,
                    color: Colors.white,
                    radius: 100,
                    borderWidth: 1,
                    borderColor: ColorsConfig.ffeceef2,
                    onPressed: () {
                      RouterUtil.instance.pop();
                    },
                  ),
                ),
                SizedBox(width: 12),
                Expanded(
                  child: CommonButton(
                    '保存',
                    radius: 100,
                    fontSize: 14,
                    padding: EdgeInsets.symmetric(horizontal: 24),
                    height: 40,
                    fontWeight: FontWeight.w600,
                    color: ColorsConfig.warningColor,
                    onPressed: () async {
                      await _saveImageToGallery(qrCode);
                      RouterUtil.instance.pop();
                    },
                  ),
                ),
              ],
            ),
          ),
        ],
      ),
      showClose: true,
    );
  }

  // 保存图片到相册
  _saveImageToGallery(Widget qrCode) async {
    showLoading();
    final imageFile = await screenshotController.captureFromWidget(qrCode);

    // 将图片文件保存到相册
    final result = await ImageGallerySaver.saveImage(imageFile);
    dismissLoading();
    if (result['isSuccess']) {
      showToast('图片保存成功');
    } else {
      showToast('图片保存失败');
    }
  }
}
